﻿
using System.Collections.Generic;
using System;
using hq;
using hcache;
using hentity;
using hdal;

namespace htaskCommon {
    public class Transfer :ZtTask {

        private IWCSServer wcsServer  = WMSDalFactray.getDal<IWCSServer>();
        ICoreTaskServer taskServer = WMSDalFactray.getDal<ICoreTaskServer>();
        public Transfer() {
            time = 500;
        }
      
        /// <summary>
        /// 
        /// </summary>
        protected override bool onlyOneTimeAsync() {
            base.onlyOneTimeAsync();
            LocCache.add(taskServer.GetAll<CoreWhLoc>());
            List<BStn> physicalStns = wcsServer.GetPhysicalStns();
            List<BStnRelation> physicalStnRelations = wcsServer.GetPhysicalStnRelation();
            foreach( BStn physicalStn in physicalStns ) {
                TransferConfigFacade.addTransferRange(physicalStn);
            }
            foreach( BStnRelation physical in physicalStnRelations ) {
                TransferConfigFacade.addQueue(physical);
            }
            List<CoreTaskParam> pars = taskServer.GetAll<CoreTaskParam>( String .Format( " and status>={0} and status< {1}",(int)CoreTaskParamStatus.下发完成 ,( int ) CoreTaskParamStatus.执行完成));

            foreach( CoreTaskParam p in pars ) {
                CompleteParamCache.add2Replace(p);
            }
           


            List<CoreTaskParam> paramList = CompleteParamCache.getWokingCompleteParam();
            foreach( CoreTaskParam p in paramList ) {
                CoreTask complete = taskServer.getById<CoreTask>(p.taskId);
               
                CacheFacade.splitParam(p ,complete);

            }


          
            return true;
        }
        public override void excute() {
            List<CoreTaskParam> list = taskServer.getWorkingTaskParam();
            if( null == list || list.Count == 0 )
                return;
            foreach( CoreTaskParam param in list ) {
                oneParam(param ,list);
            }
        }

        void oneParam(CoreTaskParam param ,List<CoreTaskParam> list) {

            for( int i = 0;i < list.Count;i++ ) {
                CoreTaskParam realyParam = list[i];
                if( param.relyParamId == realyParam.id && realyParam.status < ( int ) CoreTaskParamStatus.下发完成 ) {
                    return;
                }
            }

            CompleteParamCache.add2Replace(param);
            bool split = taskServer.splitParam(param.id);
            
            if( split ) {
                taskServer.updateTaskParamStatus(param.id ,CoreTaskParamStatus.下发完成);
            }
        }




    }
}
